package battlecruiser.player;

import battlecruiser.board.ObservableBoard;
import battlecruiser.board.ObserverBoard;
import battlecruiser.shooter.ComputerShooter;
import battlecruiser.shooter.Shooter;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * 1. Has Fleet of Ships that are displayed on ObservableBoard.
 * 2. Ships of an enemy are displayed on ObserverBoard.
 * 3. Should place it's ships before the Game starts.
 * 4. Shoots at enemy ships. Shot result are displayed on ObserverBoard
 *
 * @author Yakiv Tymoshenko
 * @since 01.09.12
 */
public class Player {

    private static Logger LOG = Logger.getLogger(Player.class);

    private String name;

    private ObservableBoard observableBoard;
    private ObserverBoard observerBoard;
    private Shooter shooter;

    //TODO add better builder
    public Player(String name) {
        this.name = name;
    }

    public void prepareFleet() {
        observableBoard = new ObservableBoard();
    }

    public void prepareShooter(ObserverBoard observerBoard) {
        shooter = new ComputerShooter(observerBoard);
    }


    /*public void addShip(Ship ship) {
        shipyard.deployShip(ship);
    }*/

    public void shoot() throws IOException {
        shooter.shoot();
        /*
        Coordinate target = targetReader.readTarget();
        if (target != null) {
            LOG.debug("Player \"" + name + "\" shoots at " + target);
            if (enemy.getFleet().reportDamage(target).getShotResult() == ShotResult.HIT) {
                LOG.debug("Player \"" + name + "\" HITs at " + target);
                shoot();
            } else {
                LOG.debug("Player \"" + name + "\" MISSes at " + target);
            }
        }
        */

    }

    public boolean isDefeated() {
        return observableBoard.getFleet().isEmpty();
    }

    public String getName() {
        return name;
    }

    public ObservableBoard getObservableBoard() {
        return observableBoard;
    }

    public void setObservableBoard(ObservableBoard observableBoard) {
        this.observableBoard = observableBoard;
    }

    public ObserverBoard getObserverBoard() {
        return observerBoard;
    }

    public void setObserverBoard(ObserverBoard observerBoard) {
        this.observerBoard = observerBoard;
    }
}
